package com.ericsson.eritask.service.impl;

import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ericsson.eritask.dao.ImageDAO;
import com.ericsson.eritask.dao.UserDAO;
import com.ericsson.eritask.domain.User;
import com.ericsson.eritask.dto.UserServiceDTO;
import com.ericsson.eritask.service.UserService;


@Service
public class UserServiceImpl implements UserService {

	@Autowired
	ImageDAO imageDao;

	@Autowired
	UserDAO userDao;


	@Override
	public String uploadImage(InputStream image, String signum,String fileName) {
		// call mongodb dao with the stream and store the id returned by dao
		String imageId = imageDao.insertImage(image,fileName);
		// call user dao to update the image id column with the image id stored in previous step pass the signum of the corresponding user
		userDao.updateUserImage(imageId, signum);


		return imageId;
	}

	@Override
	public void createUser(UserServiceDTO userDto) {


	}

	@Override
	public InputStream retrieveImage(String signum) {
		String imageId = userDao.getImageId(signum);
		InputStream is = imageDao.getImgStreamById(imageId);
		System.out.println("input stream retrieved for image id "+imageId+" is "+is);


		return is;
	}

	@Override
	public User findByName(String userName) {
		User user = userDao.findByUserName(userName);
		return user;
	}

}
